package com.wly.datapool.define
{
	import com.wly.utils.MyPoint;

	public class DirDefine
	{
		static public const DIR_UP:int    = 1;
		static public const DIR_DOWN:int  = 2;
		static public const DIR_RIGHT:int = 3;
		static public const DIR_LEFT:int  = 4;
		
		static public const DIR_UNVALID:int = 0;
		
		static public function cacuateDir(srcPos:MyPoint, dstPos:MyPoint):int
		{
			var distanceX:int;
			var distanceY:int;
			
			if(!srcPos || !dstPos)
			{
				return DIR_UNVALID;
			}
			
			if(srcPos.x!=dstPos.x && srcPos.y!=dstPos.y)
			{
				trace("UNVALID dstPos");
				return DIR_UNVALID;
			}
			
			distanceX = dstPos.x-srcPos.x;
			
			if(distanceX>0)
			{
				return DIR_RIGHT;
			}
			else if(distanceX<0)
			{
				return DIR_LEFT;
			}
			
			distanceY = dstPos.y-srcPos.y;
			if(distanceY>0)
			{
				return DIR_DOWN;
			}
			else if(distanceY)
			{
				return DIR_UP;
			}
			
			return DIR_UNVALID;
		}
		
		static public function caculateSpeed(srcPos:MyPoint, dstPos:MyPoint, speed:int):MyPoint
		{
			var speedPoint:MyPoint;
			var disX:int;
			var disY:int;
			var distance:Number;
			
			if(!srcPos && !dstPos)
			{
				return null;
			}
			
			speedPoint = new MyPoint();
			if (srcPos.equals(dstPos))
			{
				speedPoint.x = 0;
				speedPoint.y = 0;
			}
			else if(srcPos.x == dstPos.x)
			{
				speedPoint.x = 0;
				if (dstPos.y > srcPos.y)
				{
					speedPoint.y = speed;
				}
				else
				{
					speedPoint.y = -speed;
				}
			}
			else if(srcPos.y == dstPos.y)
			{
				speedPoint.y = 0;
				if (dstPos.x > srcPos.x)
				{
					speedPoint.x = speed;
				}
				else
				{
					speedPoint.x = -speed;
				}
			}
			else
			{
				disX = dstPos.x - srcPos.x;
				disY = dstPos.y - srcPos.y;
				distance = MyPoint.distance(srcPos, dstPos);
				speedPoint.x = speed*disX/distance;
				speedPoint.y = speed*disY/distance;
			}
			return speedPoint;
		}
	}
}